package simple_bbs.admin;

import act.cli.Command;
import act.cli.Required;
import org.osgl.$;
import org.osgl.util.N;
import org.osgl.util.S;
import simple_bbs.model.Message;
import simple_bbs.model.User;

import javax.inject.Inject;
import java.util.List;

/**
 * Generate test data
 */
public class AdminConsole {

    @Inject
    private User.Dao userDao;

    @Inject
    private Message.Dao msgDao;

    @Command(value = "data.gen", help = "Generate test data")
    public String genTestData(
            @Required("Number of record to be generated") int n
    ) {
        if (0 == userDao.count()) {
            return "Error: no login user found";
        }
        List<User> users = userDao.findAllAsList();
        while (n-- > 0) {
            generateOne($.random(users));
        }
        return "Test data generated";
    }

    private void generateOne(User user) {
        Message message = new Message();
        message.author = user.email;
        message.subject = S.random(N.randInt(20) + 10);
        message.body = S.random(N.randInt(400) + 10);
        msgDao.save(message);
    }

}
